package com.example.seabedmusic.utils;

import com.example.seabedmusic.entity.Playlist;
import com.example.seabedmusic.entity.Song;

import java.util.ArrayList;
import java.util.List;

public class PlaylistManager {
    private static PlaylistManager instance;
    private List<Playlist> playlists;
    private long playlistIdCounter = 1;
    private static final String FAVORITE_PLAYLIST_ID = "favorite_001";

    private PlaylistManager() {
        playlists = new ArrayList<>();
        // 初始化默认歌单
        initializeDefaultPlaylists();
    }

    public static synchronized PlaylistManager getInstance() {
        if (instance == null) {
            instance = new PlaylistManager();
        }
        return instance;
    }

    private void initializeDefaultPlaylists() {
        // 创建"我喜欢的"歌单
        Playlist favoritePlaylist = new Playlist(
                FAVORITE_PLAYLIST_ID,
                "我喜欢的",
                "我喜欢的音乐",
                ""
        );
        playlists.add(favoritePlaylist);

        // 创建默认歌单
        Playlist defaultPlaylist = new Playlist(
                String.valueOf(playlistIdCounter++),
                "我的歌单1",
                "默认歌单",
                ""
        );
        playlists.add(defaultPlaylist);
    }



    /**
     * 获取所有歌单
     */
    public List<Playlist> getAllPlaylists() {
        return new ArrayList<>(playlists);
    }

    /**
     * 根据ID获取歌单
     */
    public Playlist getPlaylistById(String id) {
        for (Playlist playlist : playlists) {
            if (playlist.getId().equals(id)) {
                return playlist;
            }
        }
        return null;
    }

    /**
     * 创建新歌单
     */
    /**
     * 创建新歌单（简化版）
     */
    public Playlist createPlaylist(String name) {
        return createPlaylist(name, "");
    }

    /**
     * 创建新歌单
     */
    public Playlist createPlaylist(String name, String description) {
        // 检查歌单名称是否已存在
        for (Playlist playlist : playlists) {
            if (playlist.getName().equals(name)) {
                return null; // 歌单名称已存在
            }
        }

        Playlist playlist = new Playlist(
                String.valueOf(playlistIdCounter++),
                name,
                description,
                ""
        );
        playlists.add(playlist);
        return playlist;
    }

    /**
     * 获取用户创建的歌单（排除"我喜欢的"）
     */
    public List<Playlist> getUserPlaylists() {
        List<Playlist> userPlaylists = new ArrayList<>();
        for (Playlist playlist : playlists) {
            if (!FAVORITE_PLAYLIST_ID.equals(playlist.getId())) {
                userPlaylists.add(playlist);
            }
        }
        return userPlaylists;
    }

    /**
     * 根据名称获取歌单
     */
    public Playlist getPlaylistByName(String name) {
        for (Playlist playlist : playlists) {
            if (playlist.getName().equals(name)) {
                return playlist;
            }
        }
        return null;
    }

    /**
     * 删除歌单
     */
    public boolean deletePlaylist(String id) {
        // 不允许删除"我喜欢的"歌单
        if (FAVORITE_PLAYLIST_ID.equals(id)) {
            return false;
        }

        Playlist playlist = getPlaylistById(id);
        if (playlist != null) {
            return playlists.remove(playlist);
        }
        return false;
    }

    /**
     * 添加歌曲到指定歌单
     */
    public boolean addSongToPlaylist(String playlistId, Song song) {
        Playlist playlist = getPlaylistById(playlistId);
        if (playlist != null && song != null) {
            // 检查歌曲是否已经存在
            if (playlist.containsSong(song)) {
                return false; // 歌曲已存在
            }
            playlist.addSong(song);
            return true;
        }
        return false;
    }

    /**
     * 从歌单移除歌曲
     */
    public boolean removeSongFromPlaylist(String playlistId, Song song) {
        Playlist playlist = getPlaylistById(playlistId);
        if (playlist != null && song != null) {
            return playlist.removeSong(song);
        }
        return false;
    }

    /**
     * 根据歌曲ID从歌单移除歌曲
     */
    public boolean removeSongFromPlaylistById(String playlistId, String songId) {
        Playlist playlist = getPlaylistById(playlistId);
        if (playlist != null && songId != null) {
            return playlist.removeSongById(songId);
        }
        return false;
    }

    /**
     * 获取"我喜欢的"歌单
     */
    public Playlist getFavoritePlaylist() {
        return getPlaylistById(FAVORITE_PLAYLIST_ID);
    }

    /**
     * 添加歌曲到"我喜欢的"
     */
    public boolean addToFavorite(Song song) {
        if (song == null) return false;

        Playlist favoritePlaylist = getFavoritePlaylist();
        if (favoritePlaylist == null) return false;

        // 检查是否已经存在
        if (favoritePlaylist.containsSong(song)) {
            return false; // 已经存在
        }

        // 添加到"我喜欢的"
        favoritePlaylist.addSong(song);
        song.setFavorite(true);
        return true;
    }

    /**
     * 从"我喜欢的"移除歌曲
     */
    public boolean removeFromFavorite(Song song) {
        if (song == null) return false;

        Playlist favoritePlaylist = getFavoritePlaylist();
        if (favoritePlaylist == null) return false;

        boolean removed = favoritePlaylist.removeSong(song);
        if (removed) {
            song.setFavorite(false);
        }
        return removed;
    }

    /**
     * 检查歌曲是否在"我喜欢的"中
     */
    public boolean isFavorite(Song song) {
        if (song == null) return false;

        Playlist favoritePlaylist = getFavoritePlaylist();
        if (favoritePlaylist == null) return false;

        return favoritePlaylist.containsSong(song);
    }

    /**
     * 获取"我喜欢的"歌曲数量
     */
    public int getFavoriteCount() {
        Playlist favoritePlaylist = getFavoritePlaylist();
        return favoritePlaylist != null ? favoritePlaylist.getSongCount() : 0;
    }

    /**
     * 根据歌曲ID查找歌曲
     */
    public Song findSongById(String songId) {
        for (Playlist playlist : playlists) {
            for (Song song : playlist.getSongs()) {
                if (song.getId().equals(songId)) {
                    return song;
                }
            }
        }
        return null;
    }

    /**
     * 清空歌单
     */
    public boolean clearPlaylist(String playlistId) {
        // 不允许清空"我喜欢的"歌单
        if (FAVORITE_PLAYLIST_ID.equals(playlistId)) {
            return false;
        }

        Playlist playlist = getPlaylistById(playlistId);
        if (playlist != null) {
            playlist.setSongs(new ArrayList<>());
            return true;
        }
        return false;
    }

    /**
     * 获取歌单中的歌曲数量
     */
    public int getPlaylistSongCount(String playlistId) {
        Playlist playlist = getPlaylistById(playlistId);
        return playlist != null ? playlist.getSongCount() : 0;
    }

    /**
     * 更新歌单信息
     */
    public boolean updatePlaylist(String playlistId, String name, String description) {
        Playlist playlist = getPlaylistById(playlistId);
        if (playlist != null) {
            playlist.setName(name);
            playlist.setDescription(description);
            return true;
        }
        return false;
    }

    /**
     * 检查歌单是否存在
     */
    public boolean playlistExists(String playlistId) {
        return getPlaylistById(playlistId) != null;
    }

    /**
     * 获取所有包含指定歌曲的歌单
     */
    public List<Playlist> getPlaylistsContainingSong(Song song) {
        List<Playlist> containingPlaylists = new ArrayList<>();
        if (song == null) return containingPlaylists;

        for (Playlist playlist : playlists) {
            if (playlist.containsSong(song)) {
                containingPlaylists.add(playlist);
            }
        }
        return containingPlaylists;
    }

    /**
     * 检查歌曲是否在指定歌单中
     */
    public boolean isSongInPlaylist(String playlistId, Song song) {
        Playlist playlist = getPlaylistById(playlistId);
        return playlist != null && playlist.containsSong(song);
    }
}